/* eslint-disable no-unused-vars */
import {
    getUserList,
    getRoleList,
    addUser,
    addRole,
    modifyUser,
    modifyPassword,
    modifyUserStatus,
    modifyRole,
    modifyRoleStatus,
    deleteUser,
    deleteRole,
    getRoleTree,
    postRoleTree,
    validUser
} from '@/api/system'
import router, { resetRouter } from '@/router'

const state = {
    current: 1,
    pagesize: 10,
    usertotal: 0,
    roletotal: 0,
    logtotal: 0,
    userid: '',
    roleid: '',
    configid: '',
    grouparr: [],
    userarr: [],
    userlist: [],
    rolelist: [],
    loglist: [],
    treelist: [],
    posttreelist: [],
    checkedlist: [],
    bnode: '',
    count: 1
}

const mutations = {
    SET_BNODE: (state, bnode) => {
        state.bnode = bnode
    },
    SET_COUNT: (state, count) => {
        state.count = count
    },
    SET_USERID: (state, userid) => {
        state.userid = userid
    },
    SET_ROLEID: (state, roleid) => {
        state.roleid = roleid
    },
    SET_CONFIGID: (state, configid) => {
        state.configid = configid
    },
    SET_PAGE: (state, current) => {
        state.current = current
    },
    SET_ROWS: (state, pagesize) => {
        state.pagesize = pagesize
    },
    SET_USERTOTAL: (state, usertotal) => {
        state.usertotal = usertotal
    },
    SET_ROLETOTAL: (state, roletotal) => {
        state.roletotal = roletotal
    },
    SET_GROUPARR: (state, grouparr) => {
        state.grouparr = grouparr
    },
    SET_USERARR: (state, userarr) => {
        state.userarr = userarr
    },
    SET_USERLIST: (state, userlist) => {
        state.userlist = userlist
    },
    SET_ROLELIST: (state, rolelist) => {
        state.rolelist = rolelist
    },
    SET_TREELIST: (state, treelist) => {
        state.treelist = treelist
    },
    SET_CHECKEDLIST: (state, checkedlist) => {
        state.checkedlist = checkedlist
    },
    SET_POSTTREELIST: (state, posttreelist) => {
        state.posttreelist = posttreelist
    }

}

const actions = {
    /* 获取用户列表*/
    getUserList({ commit, state }, args) {
        const arr = []
        return new Promise((resolve, reject) => {
            getUserList(state.current, state.pagesize, args).then(response => {
                const { data, total } = response
                // commit('SET_USERLIST', data.rows)
                // commit('SET_USERTOTAL', data.total)
                commit('SET_USERLIST', data)
                commit('SET_USERTOTAL', total)
                data.forEach(item => {
                    arr.push({ label: item.account, value: item.userId })
                })
                commit('SET_USERARR', arr)
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },

    /* 获取角色列表*/
    getRoleList({ commit, state }, args) {
        const arr = []
        return new Promise((resolve, reject) => {
            getRoleList(state.current, state.pagesize, args).then(response => {
                const { data, total } = response
                // commit('SET_ROLELIST', data.rows)
                // commit('SET_ROLETOTAL', data.total)
                commit('SET_ROLELIST', data)
                commit('SET_ROLETOTAL', total)
                data.forEach(item => {
                    arr.push({ label: item.groupName, value: item.groupId })
                })
                commit('SET_GROUPARR', arr);
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },

    /* 新增用户*/
    addUser({ commit, state }, userData) {
        return new Promise((resolve, reject) => {
            addUser(userData).then(response => {
                const { data } = response
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },
    /* 新增角色*/
    addRole({ commit, state }, roleData) {
        return new Promise((resolve, reject) => {
            addRole(roleData).then(response => {
                const { data } = response
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },

    /* 修改用户*/
    modifyUser({ commit, state }, userData) {
        return new Promise((resolve, reject) => {
            modifyUser(userData).then(response => {
                const { data } = response
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },
    /* 修改用户状态 */
    modifyUserStatus({ commit, state }, userData) {
        return new Promise((resolve, reject) => {
            modifyUserStatus(userData).then(response => {
                const { data } = response
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },
    /* 修改用户密码 */
    modifyPassword({ commit, state }, userData) {
        return new Promise((resolve, reject) => {
            modifyPassword(userData).then(response => {
                const { data } = response
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },
    /* 修改角色*/
    modifyRole({ commit, state }, roleData) {
        return new Promise((resolve, reject) => {
            modifyRole(roleData).then(response => {
                const { data } = response
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },
    /* 修改角色状态 */
    modifyRoleStatus({ commit, state }, roleData) {
        return new Promise((resolve, reject) => {
            modifyRoleStatus(roleData).then(response => {
                const { data } = response
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },
    /* 删除用户*/
    deleteUser({ commit, state }) {
        return new Promise((resolve, reject) => {
            deleteUser(state.userid).then(response => {
                const { data } = response
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },
    /* 删除角色*/
    deleteRole({ commit, state }, roleData) {
        return new Promise((resolve, reject) => {
            deleteRole(state.roleid).then(response => {
                const { data } = response
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },

    /* 获取角色树形菜单*/
    getRoleTree({ commit, state }) {
        return new Promise((resolve, reject) => {
            getRoleTree(state.roleid).then(response => {
                const { data } = response
                commit('SET_TREELIST', response.data[0].children)
                commit('SET_CHECKEDLIST', response.authIds)
                commit('SET_BNODE', response.bnode || [])
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },

    /* 提交树*/
    postRoleTree({ commit, state }) {
        // const { authIds, roldId } = treeInfo
        const treeInfo = {
            authIds: state.posttreelist,
            id: state.roleid,
            bnode: state.bnode
        }
        return new Promise((resolve, reject) => {
            postRoleTree(treeInfo).then(response => {
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },
    validUser({ commit, state }, treeInfo) {
        //const { authIds, roldId } = treeInfo
        return new Promise((resolve, reject) => {
            validUser(state.posttreelist).then(response => {
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
